<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
label,input {
	display: block;
	margin-top: 10px;
}

#actions a {
	margin-right: 5px;
}
</style>
<script type="text/javascript" src="../js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../js/jquery.dform-1.0.1.js"></script>
<script type="text/javascript" src="../js/dasding.js"></script>

<script type="text/javascript">
	function getURLParameter(name) {
		return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(
				location.search) || [ , null ])[1]);
	}

	function hanleActionData(actiondata) {
		$.ajax({
			url : "/dasding/api/actions/categories/find",
			success : function(data) {
				var categoriesString = "";
				$(data.actionCategory).each(
						function() {
							var value = "";
							if (actiondata.category.id == this.id) {
								value = '{"selected" : "selected", "html" : "'
										+ this.name + '"}';
							} else {
								value = '"' + this.name + '"';
							}
							categoriesString = categoriesString + ',' + '"'
									+ this.id + '"' + ':' + value;
						});

				categoriesString = '{' + categoriesString.substring(1) + "}";
				createForm("/dasding/api/actions/id/" + actiondata.id, "PUT",
						actiondata, JSON.parse(categoriesString));
			}
		});
	}

	function hanleNewAction(eventdata) {
		$.ajax({
			url : "/dasding/api/actions/categories/find",
			success : function(data) {
				var categoriesString = "";
				$(data.actionCategory).each(
						function() {
							categoriesString = categoriesString + ',' + '"'
									+ this.id + '"' + ':"' + this.name + '"';
						});

				categoriesString = '{' + categoriesString.substring(1) + "}";
				createForm("/dasding/api/actions", "POST", {
					"name" : "",
					"time" : eventdata.time,
					"location" : {
						"id" : eventdata.location.id
					},
					"event" : {
						"id" : eventdata.id
					}
				}, JSON.parse(categoriesString));
			}
		});
	}

	function createForm(url, method, actiondata, categories) {
		console.log(actiondata);
		$("#content").dform({
			"action" : url,
			"method" : method,
			"html" : [ {
				"type" : "p",
				"html" : "Edit Action"
			}, {
				"name" : "name",
				"id" : "name",
				"caption" : "Action Name",
				"type" : "text",
				"value" : actiondata.name
			}, {
				"name" : "description",
				"id" : "description",
				"caption" : "Action Description",
				"type" : "textarea",
				"value" : actiondata.description
			}, {
				"name" : "categoryid",
				"id" : "categegoryid",
				"caption" : "Action Category",
				"type" : "select",
				"options" : categories
			}, {
				"name" : "time",
				"id" : "time",
				"caption" : "Action Time",
				"type" : "text",
				"value" : actiondata.time
			}, {
				"name" : "locationid",
				"id" : "locationid",
				"caption" : "Action Location",
				"type" : "text",
				"value" : actiondata.location.id
			}, {
				"name" : "eventid",
				"id" : "eventid",
				"type" : "hidden",
				"value" : actiondata.event.id
			}, {
				"type" : "submit",
				"name" : "save",
				"value" : "Save"
			} ]
		});

		//browser do not support http methods put and delete
		$('#content:first-child').submit(function(event) {
			submitForm(event);
		});
	}
</script>
<script type="text/javascript">
	$(document)
			.ready(
					function() {
						if (getURLParameter('actionid') != 'null') {
							$.ajax({
								url : "/dasding/api/actions/id/"
										+ getURLParameter('actionid'),
								success : function(data) {
									hanleActionData(data);
								}
							});
							$('#actions')
									.append(
											'<a href="/dasding/api/actions/id/'
													+ getURLParameter('actionid')
													+ '" onclick="deleteObject(event, this, \'showActions.html\')")">Delete</a>');
							$('#actions')
									.append(
											'<a href="/dasding/api/actions/id/'
													+ getURLParameter('actionid')
													+ '/participate" onclick="updatedObject(event, this, \'addAction?actionid='
													+ getURLParameter('actionid')
													+ '\')">Participate</a>');
							$('#actions').append(
									'<a href="/dasding/api/actions/id/'
											+ getURLParameter('actionid')
											+ '">WillHappen</a>');

						}
						if (getURLParameter('eventid') != 'null') {
							$.ajax({
								url : "/dasding/api/events/id/"
										+ getURLParameter('eventid'),
								success : function(data) {
									hanleNewAction(data);
								}
							});
						}
					});
</script>
</head>
<body>
	<div id="content"></div>
	<div id="actions" class="actions"></div>
</body>
</html>